package com.hzeport.bgs22emduty.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hzeport.bgs22emduty.entity.BaseDeptInfo;
import com.hzeport.bgs22emduty.entity.BaseDeptType;
import com.hzeport.bgs22emduty.outbound.BaseDeptInfoRepository;
import com.hzeport.bgs22emduty.outbound.BaseDeptTypeRepository;
import com.hzeport.bgs22emduty.outbound.db.BaseDeptInfoRepositoryImpl;
import com.hzeport.bgs22emduty.service.BaseDeptInfoService;
import com.masa.common.data.base.BaseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * ${comments}
 *
 * @author code generator
 * @date 2022-12-02 11:21:59
 */
@Service
@Slf4j
public class BaseDeptInfoServiceImpl extends BaseServiceImpl<BaseDeptInfoRepositoryImpl, BaseDeptInfo> implements BaseDeptInfoService {

    @Resource
    private BaseDeptTypeRepository baseDeptTypeRepository;
    @Resource
    private BaseDeptInfoRepository baseDeptInfoRepository;

    @Override
    public List<BaseDeptInfo> deptSList(String customId) {
//        List<BaseDeptType> baseDeptTypes=baseDeptTypeRepository.list(new LambdaQueryWrapper<BaseDeptType>()
//                .notLike(BaseDeptType::getSupervisorname,"\\")
//                .notLike(BaseDeptType::getSupervisorname,"/")
//                .isNotNull(BaseDeptType::getThirdId));
//        //获取该关区id下所有的部门
//        List<String> ids=baseDeptTypes.stream().map(BaseDeptType::getThirdId).collect(Collectors.toList());
        List<BaseDeptInfo> childern = baseDeptInfoRepository.list(new LambdaQueryWrapper<BaseDeptInfo>()
        .eq(BaseDeptInfo::getCustomId,customId));
//        baseDeptInfos=baseDeptInfos.stream().filter(i->i!=null&&!ids.contains(i.getId())).collect(Collectors.toList());
//        BaseDeptInfo baseDeptInfo=baseDeptInfoRepository.getById(customId);
//        if (baseDeptInfo==null){
//            return new ArrayList<>();
//        }
//        List<BaseDeptInfo> childern=new ArrayList<>();
//        getChild(baseDeptInfo,childern,baseDeptInfos);
        return childern;
    }

    private void  getChild(BaseDeptInfo baseDeptInfo, List<BaseDeptInfo> childrens,  List<BaseDeptInfo> deptInfos) {

        List<BaseDeptInfo> myChildren=deptInfos.stream().filter(i->i.getParentid()!=null&&i.getParentid().equals(baseDeptInfo.getId())).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(myChildren)){
            childrens.addAll(myChildren);
            for (BaseDeptInfo child:myChildren){
                getChild(child,childrens,deptInfos);
            }
        }else {
            return ;
        }
    }
}
